package cn.itcast.usermanage.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.usermanage.pojo.EasyUIResult;
import cn.itcast.usermanage.pojo.User;
import cn.itcast.usermanage.service.UserService;



	@RequestMapping("user")
	@Controller
	public class UserController {	
@Autowired		
private UserService userService;	
		
		@RequestMapping(value="users")
		public String toUsers(){			
			return "users";
		}
		
		@ResponseBody
		@RequestMapping("list")
		public EasyUIResult queryUsersByPage(@RequestParam("page")Integer pageNum, @RequestParam("rows")Integer pageSize){
			
			return this.userService.queryEasyUIResult(pageNum, pageSize);
		}	
		
		
		@RequestMapping(value="/page/{pageName}")
		public  String  toUserAdd(@PathVariable("pageName")String pageName){
			return pageName;
		}
		
		
		
		@RequestMapping(value="save")
		@ResponseBody
		public Map<String, Object> addUser(User user){
			Map<String, Object> map = new HashMap<>();
			try {
				Boolean b = this.userService.addUser(user);
				if (b) {
					map.put("status", "200");
				} else {
					map.put("status", "500");
				}
			} catch (Exception e) {
				map.put("status", "500");
				e.printStackTrace();
			}		
			return map;
		}
		
		
		///0000000000000000000000000000///
		@RequestMapping(value="show35")
		public String test35(Model model,Date data){
			model.addAttribute("msg", data);
			return "hello";
		}
		
		
		@RequestMapping("delete")
		@ResponseBody
		public Map<String,Object> deleteUser(@RequestParam("ids")String []  ids ){			
			HashMap<String, Object> map = new HashMap<String,Object>();		
		try{	
			Boolean b =this.userService.deleteUser(ids);
			if(b){
			map.put("status", 200);	
			}else{
			map.put("status", 500);	
			}
		}catch(Exception e){
			map.put("status", 500);
			e.printStackTrace();
			}
			return map;	
		}
		
		@RequestMapping(value="export/excel")
		public String exportExcel(@RequestParam("page") Integer pageNum, @RequestParam("rows")Integer pageSize, Model model){
			EasyUIResult easyUIResult =this.userService.queryUserListByPage(pageNum, pageSize);
			model.addAttribute("userList", easyUIResult.getRows());
			return "userExcelView";	
		}
		
		
		
		
		
		
	}
	
	
	

